﻿(function ($) {
    $.QueryString = (function(a) {
        if (a == "") return {};
        var b = {};
        for (var i = 0; i < a.length; ++i) {
            var p = a[i].split('=');
            if (p.length != 2) continue;
            b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
        }
        return b;
    })(window.location.search.substr(1).split('&'));
})(jQuery);

var handler = function (pathname) {
    var activityLog = function(xmlHttpRequest) {
        var ex = eval("(" + xmlHttpRequest.responseText + ")");
        alert("failed\nDetail: " + ex.Message);
    };

    return {
        article: {
            get: function (id) {
                var result;
                $.ajax({
                    type: "POST",
                    url: pathname + "/Get",
                    dataType: "json",
                    contentType: "application/json",
                    async: false,
                    data: JSON.stringify({ id: id }),
                    success: function (data) {
                        if (data.d !== null) {
                            result = data.d;
                        }
                    },
                    error: function (xmlHttpRequest) {
                        activityLog(xmlHttpRequest);
                        result = null;
                    }
                });

                return result;
            },
            save: function (model) {
                var result;
                $.ajax({
                    type: "POST",
                    url: pathname + '/Save',
                    dataType: "json",
                    contentType: "application/json",
                    data: JSON.stringify({ model: model }),
                    success: function (data) {
                        result = data;
                    },
                    error: function (xmlHttpRequest) {
                        activityLog(xmlHttpRequest);
                        result = false;
                    }
                });

                return result;
            },
            remove: function(id) {
                var result;
                $.ajax({
                    type: "POST",
                    url: pathname + "/DeleteArticle",
                    data: JSON.stringify({ id: id }),
                    contentType: "application/json",
                    success: function(data) {
                        result = data;
                    },
                    error: function (xmlHttpRequest) {
                        activityLog(xmlHttpRequest);
                        result = false;
                    }
                });

                return result;
            }
        },
        category: {
            remove: function (model) {
                var result;
                $.ajax({
                    type: "POST",
                    url: pathname + "/DeleteCategory",
                    data: JSON.stringify({ model: model }),
                    contentType: "application/json",
                    success: function(data) {
                        result = data;
                    },
                    error: function (xmlHttpRequest) {
                        activityLog(xmlHttpRequest);
                        result = false;
                    }
                });

                return result;
            }
        }
    };
}